package com.shi.springboot.comment.controller;

import com.shi.springboot.comment.entity.SysUser;
import com.shi.springboot.comment.service.SysUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author stone
 * @since 2021-07-08
 */
@RestController
@RequestMapping("/sys/user")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    @ApiOperation("测试")
    @GetMapping("/test")
    public String test(){
        return "hello";
    }

    @ApiOperation("根据ID查User")
    @GetMapping("/finduser")
    public SysUser findUSer(@RequestParam String id){
        SysUser user = sysUserService.getById(id);
        return user;
    }
    @ApiOperation("查询所以User")
    @GetMapping("/alluser")
    public List<SysUser>allUser(){
        return sysUserService.list();
    }

    /**
     * 使用对象来传参
     * @param sysUser
     * @return
     */
    @ApiOperation("创建用户")
    @PostMapping("/createuser")
    public boolean createUser(@RequestBody SysUser sysUser){

        boolean bpp = sysUserService.save(sysUser);

        return bpp;
    }
    @ApiOperation("另一个创建用户")
    @PostMapping("createUser1")
    public boolean createUser(@RequestParam Map<String,String> prarm){
        SysUser sy=new SysUser();
        if(prarm.size()==1){
            sy.setName(prarm.get("name"));
            sy.setEmail(prarm.get("email"));
            sy.setLoginName(prarm.get("loginName"));
            sy.setCreateTime(LocalDateTime.now());
            sy.setCreateBy("1");
            sy.setUpdateBy("1");
            sy.setUpdateTime(LocalDateTime.now());
            sy.setPassword(prarm.get("password"));
            sy.setSalt("11111111");
            sy.setIsAdmin(0);
        }
        return sysUserService.save(sy);
    }
}
